// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef __PLANETARYLOADABLE_H_
#define __PLANETARYLOADABLE_H_

#include "planetaryscenemanager.h"
#include "planetaryoptions.h"

namespace Ogre
{
	/**
	* Abstract class for loadable scene manager objects.
	* This class is used by any class that's loaded on demand by the scene manager, such as pages, tiles etc.
	*/
	class PlanetaryLoadable
	{
	public:
		PlanetaryLoadable(PlanetarySceneManager *mgr)
		{
			mIsLoading = false;
			mIsLoaded = false;
			mIsUnloading = false;
			mTimeUntouched = 0;

			mOptions = mgr->getOptions();
		}

		/// True virtual load function
		virtual void load() = 0;
		/// True virtual unload function
		virtual void unload() = 0;

		/// Returns true if this loadable is loaded
		inline bool isLoaded() const
		{
			return mIsLoaded;
		}

		/// Returns true if this loadable is in a load queue
		inline bool isLoading() const
		{
			return mIsLoading;
		}

		/// Set if this loadable has been inserted into a load queue
		inline void setIsLoading(bool b)
		{
			mIsLoading = b;
		}

		/// Returns true if this loadable is in an unload queue
		inline bool isUnloading() const
		{
			return mIsUnloading;
		}

		/// Set if this loadable has been inserted into an unload queue
		inline void setIsUnloading(bool b)
		{
			mIsUnloading = b;
		}

		/// Touch the loadable notify it that it's in use
		inline void touch()
		{
			mTimeUntouched = mOptions->mPageUnloadFrequency;
		}

		/// Return true if this loadable has been unused long enough to be unloaded
		inline bool unloadUntouched()
		{
			if (mTimeUntouched == 0)
				return true;
			mTimeUntouched--; 
			return false;
		}

	protected:
		bool mIsLoading; /**< True if this loadable is currently in a loading queue */
		bool mIsLoaded; /**< True if this loadable is currently loaded */
		bool mIsUnloading; /**< True if this loadable is currently in a unloading queue */

		/// Keeps track of if the page is used or not
		unsigned int mTimeUntouched;

		/// Options for timervalues
		PlanetaryOptions *mOptions;
	};
}


#endif
